#include <QtGui>
#include <QDebug>
#include <math.h>
#include <QFileDialog>

#include "SageVis/sagevis.h"

//command line input format:
//      cmd     dataFile1   dataFile2   windowWidth     windowHeight
//      windowWidth and height may not be there- must check
//      dataFiles may not be there, in which case open a file browser
int main(int argc, char *argv[])
{
    //application setup
    QApplication app(argc, argv);
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));

    //GET AND SET SCENE DIMENSIONS
    float windowWidth = 1440.0;
    float windowHeight = 800.0;

    //first check to see if user has given windowWidth and windowHeight on command line
    bool gotWidthAndHeight = false;
    if( argc == 5 )//this has been specified
    {

        //qDebug() " windowWidth = " << atoi( argv[3] ) << " windowHeight = " << atoi( argv[4] ) << endl;
//        windowWidth = atoi( argv[3]);
//        windowHeight = atoi( argv[4] ); //note- will be problem if not an integer

//        if( windowWidth < 0 || windowHeight < 0 || windowWidth > 1360.0*8 || windowHeight > 1020.0*4 )
//        {
//            qDebug() << "Input on command line is:  ./Go-SageVis gffFile fastaFile windowWidth windowHeight" << endl;
//            qDebug() << "where windowWidth and windowHeight are integers." << endl;
//            exit( 1 );
//        }

//        gotWidthAndHeight = true;
   }
    if( !gotWidthAndHeight )
    {
    #if defined(Q_OS_LINUX)
        windowWidth = 1360.0*2.0;//8160;//1360.0*2.0;
        windowHeight = 768.0*2.0;//2304;//
    #endif
    #if defined(Q_WS_WIN)
        windowWidth = 1280.0;
        windowHeight = 750.0;
    #endif
    }

    //then see if data file has been given
    bool gotDataFiles = false;
    QString pathToData = "../../../../SageVis/data";
    QString gffFile = "  ";
    if( argc >= 2 )
    {
        //qDebug() " windowWidth = " << atoi( argv[3] ) << " windowHeight = " << atoi( argv[4] ) << endl;
    }

    //if( !gotDataFiles )
    //{
    #if defined(  Q_WS_WIN )
        pathToData = "./data";
       //pathToData.append( "/contigs/1201_seq.reformat.tab")
    #endif
    #if defined(  Q_OS_LINUX )
        pathToData = "./data";
    #endif
    //}

    //setup the scene
    QGraphicsScene scene;
    scene.setSceneRect(-windowWidth/2.0, -windowHeight/2.0, windowWidth, windowHeight);//-700, -350, 1400, 700);
    scene.setItemIndexMethod(QGraphicsScene::NoIndex);//QGraphicsScene::BspTreeIndex);//

    //open sagevis and launch widget- later will do this under user control
    SageVis sageVis;
    scene.addItem( &sageVis );

    //sageVis.launchWidget("spatialWidget", "/Users/jillianaurisano/Development/sagevis/sageVis/data/plot1/plot1_xy.csv");

#if defined( Q_WS_WIN )
    //sageVis.launchWidget( "bacterialContigBrowser", pathToData.append( "/contigs/1201_seq.reformat.tab"));///contigs/1201_seq_sorted_flipped.reformat.tab"));//1201_seq_sorted.reformat.tab"));//"/contigs/1201_seq.reformat.tab"));//Cry3_contigs.reformat.tab" ) );//using relative path now
    sageVis.launchWidget( "bacterialContigBrowser" );//, pathToData.append( "/contigs/1201_seq.reformat.tab"));///contigs/1201_seq_sorted_flipped.reformat.tab"));//1201_seq_sorted.reformat.tab"));//"/contigs/1201_seq.reformat.tab"));//Cry3_contigs.reformat.tab" ) );//using relative path now
#endif
#if defined( Q_OS_LINUX )
//    sageVis.launchWidget( "bacterialContigBrowser", pathToData.append( "/contigs/annonContigData.txt" ) );//using relative path now
    sageVis.launchWidget( "bacterialContigBrowser" );//, pathToData.append( "/contigs/1201_seq.reformat.tab"));///contigs/1201_seq_sorted_flipped.reformat.tab"));//1201_seq_sorted.reformat.tab"));//"/contigs/1201_seq.reformat.tab"));//Cry3_contigs.reformat.tab" ) );//using relative path now
#endif
#if defined( Q_WS_MAC  )
    sageVis.launchWidget( "bacterialContigBrowser", pathToData.append( "/contigs/annonContigData.txt" ) );//using relative path now
#endif

    sageVis.launchWidget( "colorControllerBox");

    //add scene to graphics view
    QGraphicsView view(&scene);
    view.setRenderHints(QPainter::Antialiasing |  QPainter::SmoothPixmapTransform);
    view.setBackgroundBrush(QBrush(Qt::black));

    view.setCacheMode(QGraphicsView::CacheBackground);
    view.setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);//QGraphicsView::MinimalViewportUpdate);//QGraphicsView::BoundingRectViewportUpdate);
    view.setDragMode(QGraphicsView::ScrollHandDrag);

    view.setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "BioSAGE"));
    view.resize(windowWidth,windowHeight);
    view.show();

    QTimer timer;
    QObject::connect(&timer, SIGNAL(timeout()), &scene, SLOT(advance()));
    float ms = 1000.0;
    float frames = 60.0;
    timer.start( ms / frames );//60 frames per second

    return app.exec();
}
